package com.hoshiicloud.purchase.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.purchase.Po;
import com.hoshiicloud.purchase.param.PoAddParam;
import com.hoshiicloud.purchase.param.PoQueryParam;
import com.hoshiicloud.purchase.param.PoUpdateParam;
import com.hoshiicloud.purchase.service.PoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Slf4j
@RestController
@RequestMapping("/po")
@Api(value = "采购单", tags = {"采购单"})
public class PoController {
    @Autowired
    private  PoService poService;

    /**
     * @Author:campchen
     * @Description:
     * @Date:上午 10:25 2019/6/14
     * @Param:
     * @return:
     */
    @ApiOperation(value = "采购主单分页查询")
    @PostMapping("/getPage")
    public Response<IPage<Po>> getPage(@RequestBody PoQueryParam param) {
        try {
            QueryWrapper<Po> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getCompanyId() != null), Po::getCompanyId, param.getCompanyId())
                    .eq((param.getPlatformId() != null), Po::getPlatformId, param.getPlatformId())
                    .eq((param.getStoreId() != null),  Po::getStoreId, param.getStoreId())
                    .eq((param.getSource() != null),  Po::getSource,param.getSource())
                    .eq((param.getType() != null), Po:: getType,param.getType())
                    .eq(StringUtils.isNotBlank(param.getPoNo()),  Po::getPoNo, param.getPoNo())
                    .eq((param.getPoDate() != null),  Po::getPoDate, param.getPoDate())
                    .eq((param.getVendorId() != null), Po::getVendorId, param.getVendorId())
                    .eq((param.getVendorWhid() != null), Po::getVendorWhid, param.getVendorWhid())
                    .eq((param.getPlatformWhid() != null), Po::getPlatformWhid, param.getPlatformWhid())
                    .eq((param.getStoreWhid() != null),  Po::getStoreWhid, param.getStoreWhid())
                    .eq((param.getOrderAmount() != null),  Po::getOrderAmount, param.getOrderAmount())
                    .eq((param.getOrderCount() != null), Po::getOrderCount, param.getOrderCount())
                    .eq((param.getMemberId() != null),  Po::getMemberId, param.getMemberId())
                    .eq(StringUtils.isNotBlank(param.getAddress()),  Po::getAddress, param.getAddress())
                    .eq(StringUtils.isNotBlank(param.getConsignee()),  Po::getConsignee, param.getConsignee())
                    .eq((param.getCountry() != null),  Po::getCountry, param.getCountry())
                    .eq((param.getProvince() != null),  Po::getProvince, param.getProvince())
                    .eq((param.getCity() != null), Po:: getCity, param.getCity())
                    .eq((param.getDistrict() != null), Po:: getDistrict, param.getDistrict())
                    .eq(StringUtils.isNotBlank(param.getZipcode()),  Po::getZipcode, param.getZipcode())
                    .eq(StringUtils.isNotBlank(param.getTelphone()),  Po::getTelphone, param.getTelphone())
                    .eq(StringUtils.isNotBlank(param.getMobile()), Po::getMobile, param.getMobile())
                    .eq(StringUtils.isNotBlank(param.getEmail()), Po::getEmail, param.getEmail())
                    .eq((param.getDeliveryDate() != null), Po::getDeliveryDate, param.getDeliveryDate())
                    .eq((param.getStatus() != null),  Po::getStatus, param.getStatus())
                    .eq((param.getSettleStatus() != null),  Po::getSettleStatus, param.getSettleStatus())
                    .eq((param.getSettleAmount() != null),  Po::getSettleAmount, param.getSettleAmount())
                    /**根据创建时间进行倒序排列*/
                    .orderByDesc(Po::getCreatedTime);
            IPage<Po> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
            return Response.data(poService.getPage(page,queryWrapper,key));
        } catch (Exception e) {
            log.error("采购主单分页查询异常", e);
            return Response.failed("采购主单分页查询");
        }
    }


    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(poService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public Response<String> save(@RequestBody @Valid PoAddParam param) {
        Po entity;
        try {
            entity = BeanUtils.covertType(param, Po.class);
        } catch (Exception e) {
            log.error("采购新增Bean转换异常", e);
            return Response.failed("采购新增Bean转换异常") ;
        }
        return Response.data(poService.savePo(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public Response update(@RequestBody @Valid PoUpdateParam param) {
        Po entity;
        try {
            entity = BeanUtils.covertType(param, Po.class);
        } catch (Exception e) {
            log.error("采购修改Bean转换异常", e);
            return Response.failed("采购修改Bean转换异常");
        }
        return Response.data(poService.updateById(entity));
    }

}
